{% extends "base.html" %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>样本存储管理</h2>
        <a href="{{ url_for('storage.storage_add') }}" class="btn btn-primary">
            <i class="fas fa-plus"></i> 样本入库
        </a>
    </div>

    <!-- 搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">样本编号</label>
                    <input type="text" class="form-control" name="sample_code" value="{{ sample_code }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">样本类型</label>
                    <select class="form-select" name="sample_type">
                        <option value="">所有类型</option>
                        <option value="tissue" {% if sample_type == 'tissue' %}selected{% endif %}>组织样本</option>
                        <option value="blood" {% if sample_type == 'blood' %}selected{% endif %}>血液样本</option>
                        <option value="dna" {% if sample_type == 'dna' %}selected{% endif %}>DNA样本</option>
                        <option value="rna" {% if sample_type == 'rna' %}selected{% endif %}>RNA样本</option>
                        <option value="protein" {% if sample_type == 'protein' %}selected{% endif %}>蛋白质样本</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">状态</label>
                    <select class="form-select" name="status">
                        <option value="">所有状态</option>
                        <option value="stored" {% if status == 'stored' %}selected{% endif %}>已入库</option>
                        <option value="in_transit" {% if status == 'in_transit' %}selected{% endif %}>运输中</option>
                        <option value="delivered" {% if status == 'delivered' %}selected{% endif %}>已送达</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">所在仓库</label>
                    <select class="form-select" name="warehouse_id">
                        <option value="">所有仓库</option>
                        {% for warehouse in warehouses %}
                        <option value="{{ warehouse.id }}" {% if warehouse_id|string == warehouse.id|string %}selected{% endif %}>
                            {{ warehouse.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">&nbsp;</label>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>样本编号</th>
                            <th>样本类型</th>
                            <th>数量</th>
                            <th>存储位置</th>
                            <th>温度要求</th>
                            <th>湿度要求</th>
                            <th>状态</th>
                            <th>入库时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for storage in storages %}
                        <tr>
                            <td>{{ storage.sample_code }}</td>
                            <td>
                                {% if storage.sample_type == 'tissue' %}组织样本
                                {% elif storage.sample_type == 'blood' %}血液样本
                                {% elif storage.sample_type == 'dna' %}DNA样本
                                {% elif storage.sample_type == 'rna' %}RNA样本
                                {% elif storage.sample_type == 'protein' %}蛋白质样本
                                {% else %}{{ storage.sample_type }}{% endif %}
                            </td>
                            <td>{{ storage.quantity }}</td>
                            <td>{{ storage.storage_location }}</td>
                            <td>{{ storage.temperature_requirement }}°C</td>
                            <td>{{ storage.humidity_requirement }}%</td>
                            <td>
                                {% if storage.status == 'stored' %}
                                    <span class="badge bg-success">已入库</span>
                                {% elif storage.status == 'in_transit' %}
                                    <span class="badge bg-info">运输中</span>
                                {% elif storage.status == 'delivered' %}
                                    <span class="badge bg-primary">已送达</span>
                                {% endif %}
                            </td>
                            <td>{{ storage.entry_time.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>
                                <a href="{{ url_for('storage.storage_edit', id=storage.id) }}" class="btn btn-sm btn-info">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <a href="{{ url_for('storage.storage_delete', id=storage.id) }}" 
                                   class="btn btn-sm btn-danger"
                                   onclick="return confirm('确定要删除该样本记录吗？')">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if pagination and pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('storage.storage_list', page=pagination.prev_num, sample_code=sample_code, sample_type=sample_type, status=status, warehouse_id=warehouse_id) if pagination.has_prev else '#' }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('storage.storage_list', page=page, sample_code=sample_code, sample_type=sample_type, status=status, warehouse_id=warehouse_id) }}">
                                    {{ page }}
                                </a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('storage.storage_list', page=pagination.next_num, sample_code=sample_code, sample_type=sample_type, status=status, warehouse_id=warehouse_id) if pagination.has_next else '#' }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            <div class="text-center text-muted">
                共 {{ pagination.total }} 条记录，当前第 {{ pagination.page }}/{{ pagination.pages }} 页
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 